using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using Newtonsoft.Json;

public class DataManager : MonoBehaviour
{
   public static DataManager Instance { get; private set; } // 静态Instance属性用来数据持久化

   public float MusicSettingValue; // 存储用户在SettingsDialog中修改的音乐的值
   public float SFXSettingValue = 0.34f;
   
   void Awake()
   {
      if (Instance != null)
      {
         Destroy(this.gameObject); // 销毁当前新加载的Data Manager游戏对象，只保留第一次加载的那个
         return;
      }

      Instance = this;
      DontDestroyOnLoad(gameObject); // 将第一次加载的Data Manager游戏对象缓存
   }
   
   [Serializable]
   class SaveData
   {
      public float MusicSetting;
      public float SFXSetting = 0.34f;
   }

   public void SaveMusicSetting()
   {
      SaveData data = new SaveData();
      data.MusicSetting = MusicSettingValue;

      // DataManager data = this;
      
      string json = JsonConvert.SerializeObject(data);
      File.WriteAllText(Application.persistentDataPath + "/Settings.json", json);

   }
}
